package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjFilesDao;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.query.ProjFilesQuery;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.KeyValueCondition;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 项目附件表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjFilesDaoImpl extends BaseDaoImpl<ProjFiles> implements ProjFilesDao {

    @Override
    public <P extends DbCsp> List<ProjFiles> getSearch(P para) {
        return null;
    }

    public SqlQueryMate getPageOrListSqlQueryMate(ProjFilesQuery query) {
        String table = "t_proj_files tpf " +
                " left join t_sys_user tsu on tpf.c_db_create_id = tsu.c_id ";
        String columns = "tpf.*, tsu.c_user_name";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tpf.c_db_create_date asc");
        sqlQueryMate.where(true, "tpf.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(true, "tpf.c_proj_id", ScriptConditionEnum.Eq, query.getProjId());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getProjStageId()), "tpf.c_proj_stage_id", ScriptConditionEnum.Eq, query.getProjStageId());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getProjStageTypeCode()), "tpf.c_proj_stage_type_code", ScriptConditionEnum.Eq, query.getProjStageTypeCode());
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getTypeCode()), "tpf.c_type_code", ScriptConditionEnum.Eq, query.getTypeCode());
        return sqlQueryMate;
    }

    @Override
    public List<ProjFilesVO> list(ProjFilesQuery query) {
        SqlQueryMate sqlQueryMate = getPageOrListSqlQueryMate(query);
        return selectList(sqlQueryMate, ProjFilesVO.class);
    }

    @Override
    public DataPager<ProjFilesVO> listPage(ProjFilesQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        SqlQueryMate sqlQueryMate = getPageOrListSqlQueryMate(query);
        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjFilesVO.class);
    }

    @Override
    public Boolean deleteByProjIdAndProjStageIds(String userId, String projId, List<String> projStageIds) {
        String table = "t_proj_files tpf";
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1);
        sqlUpdateMate.setColumnValue(true, "c_db_last_update_id", userId);
        sqlUpdateMate.setColumnValue(true, "c_db_last_update_date", LocalDateTime.now());
        sqlUpdateMate.where(ObjectUtil.isNotEmpty(projId), "c_proj_id", ScriptConditionEnum.Eq, projId);
        sqlUpdateMate.where(CollUtil.isNotEmpty(projStageIds), "c_proj_stage_id", ScriptConditionEnum.In, projStageIds);
        return updateSqlUpdateMate(sqlUpdateMate);
    }
}
